package cake.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cake.bean.Category;
import cake.db.MySqlDb;

//闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归柨鐔稿祹閸栤剝瀚归柨鐔告灮閹风柉鐦戦柨鐔告灮閹风兘鏁撻敓锟�
public class CategoryDao {
	private MySqlDb db=new MySqlDb();
	
	//闁岸鏁撻弬銈嗗id闁跨喐鏋婚幏宄板絿闁跨喐鏋婚幏鐑芥晸閿燂拷	
	public Category getCategory(long id)
	{
		Category category=null;
		String sql="select * from Category where id="+id;
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				category=new Category();
				category.setId(rs.getLong("id"));
				category.setName(rs.getString("name"));
				category.setRemark(rs.getString("remark"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return category;
	}
	
	//闁跨喐鏋婚幏鐑姐�闁跨喐鏋婚幏宄板絿闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归弬锟介柨鐔告灮閹风ageSize娑擄拷閺冨爼鏁撻弬銈嗗缁�椽鏁撻弬銈嗗闁跨喐鏋婚幏鐑姐�
	public List<Category> getCategorys(int currPage,int pageSize,String where)
	{
		List<Category> categorys=new ArrayList<Category>();
		String sql="select * from Category ";
		if(!where.equals(""))
		{
			sql+=" Where "+where;
		}
		if(pageSize!=0)
		{
			sql+=" limit "+((currPage-1)*pageSize)+","+pageSize;
		}
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				Category category=new Category();
				category.setId(rs.getLong("id"));
				category.setName(rs.getString("name"));
				category.setRemark(rs.getString("remark"));
				
				categorys.add(category);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return categorys;
	}
	//闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归柨鐔告灮閹风兘鏁撻敓锟�
	public boolean addCategory(Category category)
	{
		String sql="insert into category(name,remark) values('"+category.getName()+"','"+category.getRemark()+"')";
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
		
	}
	//闁跨喓娼鹃棃鈺傚闁跨喐鏋婚幏鐑芥晸閿燂拷	
	public boolean updateCategory(Category category)
	{
		Category oldcategory=getCategory(category.getId());
		
		if(oldcategory==null)
		{
			return false;
		}
		
		String sql="Update category set name='"+category.getName()+"',remark='"+category.getRemark()+"' where id"+category.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
	//閸掔娀鏁撻弬銈嗗闁跨喐鏋婚幏鐑芥晸閿燂拷	
	public  boolean deleteCategory(Category category)
	{
		Category oldcategory=getCategory(category.getId());
		
		if(oldcategory==null)
		{
			return false;
		}
		
		String sql="Delete from Category where id="+category.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}

}
